package com.cloud.control;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.cloud.bean.FileItem;
import com.mogujie.tt.R;

public class FileItemAdapter extends BaseAdapter implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{

	private List<FileItem> files;
	private LayoutInflater inflater;
	private Context context;

	public FileItemAdapter(Context context, List<FileItem> files ){
		inflater = LayoutInflater.from(context);
		this.files = files;
		this.context =context;
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public Object getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.file_item, parent, false);
			holder = new ViewHolder();
			holder.iv_fileIcon = (ImageView) convertView.findViewById(R.id.iv_file_icon);
			holder.tv_fileName = (TextView) convertView.findViewById(R.id.tv_file_name);
			holder.tv_fileMsg = (TextView) convertView.findViewById(R.id.tv_file_msg);
			holder.cv_fileChoose = (CheckBox) convertView.findViewById(R.id.icheckbox_file_operate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		FileItem item = files.get(position);
		holder.iv_fileIcon.setImageResource(item.fileIconRes);

		holder.tv_fileName.setText(item.fileName);
		if (item.fileMsg.length() != 0) {
			holder.tv_fileMsg.setVisibility(View.VISIBLE);
			holder.tv_fileMsg.setText(item.fileMsg);
		} else {
			holder.tv_fileMsg.setVisibility(View.GONE);
		}
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Toast.makeText(context,"打开"+position+"文件",Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		Toast.makeText(context,"长按打开"+position+"文件",Toast.LENGTH_SHORT).show();
			return true;
	}

	private class ViewHolder {
		ImageView iv_fileIcon;
		TextView tv_fileName;
		TextView tv_fileMsg;
		CheckBox cv_fileChoose;
	}
}